package org.abettor.pushbox.map;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.abettor.pushbox.util.ByteArrayUtil;
import org.abettor.pushbox.util.Statck;

/**
 * 进行Boxmap操作的工具类
 * @author jzh
 *
 */
public class BoxmapUtil {
	/**
	 * 把地图格式化为一维数组
	 * @param map
	 * @param withphrix 是否算上前两位的prifix
	 * @return
	 */
	public static byte[] paraMapToArray(Boxmap map,boolean withphrix){
		int width = map.getWidth();
		int height = map.getHeight();
		byte [] ret = null;
		int index = 0;
		if(withphrix){
			ret = new byte[width * height+2];
			ret[0] = (byte)width;
			ret[1] = (byte)height;
			index +=2;
		}else{
			ret = new byte[width * height];
		}
		ByteArrayUtil.change2to1(map.toByte(), ret, index);
		return ret;
	}
	/**
	 * 把一维数组格式化为地图
	 * @param array
	 * @return
	 */
	public static Boxmap paraArrayToMap(byte [] array){
		int x = array[0];
		int y = array[1];
		byte[][] buff = ByteArrayUtil.change1to2(array, 2, x, y);
		return new Boxmap(buff);
	}
	/**
	 * 把步骤列表格式化为byte叔祖
	 * @param step
	 * @return
	 */
	public static byte[] paraStatckStepToArray(Statck<Step> step){
		byte [] buf = new byte[step.size()];
		Iterator<Step> itr = step.iterator();
		int index = 0;
		while(itr.hasNext()){
			Step s = itr.next();
			buf[index++] = s.toByte();
		}
		return buf;
	}
	/**
	 * 把二进制数组格式化步骤列表
	 * @param buf
	 * @return
	 */
	public static List<Step> paraArrayToStep(byte [] buf){
		if(buf==null)
			return null;
		List<Step> stepList = new ArrayList<Step>();
		for(int i=0;i < buf.length;i++){
			Step step = new Step(buf[i]);
			stepList.add(step);
		}
		return stepList;
	}
}
